﻿#include "mysql.h"

mySql::mySql()
{
    if(QSqlDatabase::contains("qt_sql_default_connection"))
    {
        database=QSqlDatabase::database();
    }
    else
    {
        database=QSqlDatabase::addDatabase("QSQLITE");
        database.setDatabaseName("wxx.db");
    }
    insert=QString("INSERT INTO studentInfo VALUES(%1,%2,%3)");
    remove=QString("DELETE FROM studentInfo WHERE %2");
    change=QString("UPDATE studentInfo SET %1 WHERE %2");
    find=QString("SELECT * FROM studentInfo %1");
    table=QString("CREATE TABLE studentInfo(\
                  id INT,\
                  name VARCHAR(30),\
                  sex VARCHAR(30)\
                  )");
}

bool mySql::openBase()
{
    if(!database.open())
    {
        qDebug("open base error");
        return false;
    }
    else
    {
        qDebug("open success");
        return true;
    }
}

void mySql::closeBase()
{
    database.close();
    qDebug("close base");
}

bool mySql::isExist(QString &tableName)
{

    if(database.tables().contains(tableName))
    {
        return true;
    }
    else
    {
        return false;
    }
}


void mySql::findData(QString order,QVector<student*> &stuV)
{
    QSqlQuery query;
    if(!query.exec(order))
    {
        qDebug()<<"error:"<<query.lastError();
    }
    else
    {
        while(query.next())
        {
            student *stu=new student;
            stu->id=query.value(0).toInt();
            stu->name=query.value(1).toString();
            stu->sex=query.value(2).toString();
            qDebug()<<stu->id<<" "<<stu->name<<" "<<stu->sex;
            stuV<<stu;
        }
    }
}


void mySql::commonFuc(QSqlQuery &order)
{
    if(!order.exec())
    {
        qDebug()<<"error:"<<order.lastError();
    }
    else
    qDebug("execute sucessfully");
}



